﻿Public Class clsParentSelector
    Public Function Zufallszahl(ByVal min, ByVal max)
        If randomType = 1 Then
            Dim z As Single
            z = Rnd()            'Zufallszahl (liegt zwischen 0 und 1)
            Zufallszahl = Int(min + (max - min + 1) * z)
        Else
            If randInit Then RandNum = New Random
            Zufallszahl = RandNum.Next(min, max)
        End If
        Return Zufallszahl
    End Function
    Public Function GeneratePairs(ByVal pi As Single, projList As List(Of KeyValuePair(Of Single, clsProject)), popmax As Int32) As Dictionary(Of Int32, clsProjectPair)
        Try
            Dim count As Int32 = ((pi * (popmax / divider))) - 1
            If randomType = 1 Then
                If randInit Then Randomize()
            End If

            Dim tempList As HashSet(Of KeyValuePair(Of Single, clsProject)) = New HashSet(Of KeyValuePair(Of Single, clsProject))(projList)
            Dim pairList As New Dictionary(Of Int32, clsProjectPair)
            For i As Long = 0 To count
                Try
                    If tempList.Count = 0 Then
                        Exit For
                    End If
                    Dim xc As Int32 = tempList.Count - 1
                    Dim father As clsProject = tempList.ElementAt(0).Value
                    Dim randomSelectorNr As Int32
                    If xc > 0 Then
                        ' randomSelectorNr = Zufallszahl(xc / 2, xc) 'RandNum.Next(1, xc)
                        randomSelectorNr = Zufallszahl(1, xc)
                    Else
                        randomSelectorNr = 0 'last pair
                    End If
                    Dim mother As clsProject = tempList.ElementAt(randomSelectorNr).Value
                    Dim pair As New clsProjectPair
                    With pair
                        .mother = mother.Clone(False)
                        .father = father.Clone(False)
                    End With
                    pairList.Add(i, pair)
                    tempList.Remove(tempList(randomSelectorNr)) 'remove mother first

                    If xc > 0 Then
                        tempList.Remove(tempList.First)
                    End If
                Catch ex As Exception
                    Debug.Print("Warning! Selection Error")
                    Exit For
                End Try
            Next
            Return pairList
        Catch ex As Exception
            Throw
        End Try
    End Function

End Class
